package com.hongyuan.sellfitness.ui.find.view_mode;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableList;

import com.hongyuan.mvvmhabitx.binding.command.BindingAction;
import com.hongyuan.mvvmhabitx.binding.command.BindingCommand;
import com.hongyuan.sellfitness.BR;
import com.hongyuan.sellfitness.R;
import com.hongyuan.sellfitness.base.Constants;
import com.hongyuan.sellfitness.base.CustomViewModel;
import com.hongyuan.sellfitness.base.ItemViewModel;
import com.hongyuan.sellfitness.ui.find.beans.PostCommentBean;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class CommentItemViewModel extends ItemViewModel<CustomViewModel> {

    public ObservableField<PostCommentBean.ListBean> entity = new ObservableField<>();

    //给RecyclerView添加ObservableList
    public ObservableList<CommentChildItemViewModel> observableList = new ObservableArrayList<>();

    //给RecyclerView添加ItemBinding
    public ItemBinding<CommentChildItemViewModel> itemBinding = ItemBinding.of(BR.viewModel, R.layout.item_comment_child);

    public CommentItemViewModel(@NonNull CustomViewModel viewModel,PostCommentBean.ListBean bean) {
        super(viewModel);
        entity.set(bean);

        for(PostCommentBean.ListBean.ListSBean childBean : bean.getList_s()){
            CommentChildItemViewModel childItemViewModel = new CommentChildItemViewModel(viewModel,this,childBean);
            observableList.add(childItemViewModel);
        }
    }

    //评论
    public BindingCommand comment = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            viewModel.itemClick(CommentItemViewModel.this);

            if(viewModel instanceof PostDetailsViewModel){
                PostDetailsViewModel fViewModel = (PostDetailsViewModel)viewModel;

                viewModel.setParams("circle_id",String.valueOf(entity.get().getCircle_id()))
                        .setParams("cr_id_father",String.valueOf(entity.get().getCr_id()))
                        .setParams("first_cr_id",String.valueOf(entity.get().getCr_id()));
                fViewModel.uc.showEdit.call();
            }
        }
    });

    //点赞
    public BindingCommand praise = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            viewModel.itemClick(CommentItemViewModel.this);

            viewModel.setParams("cr_id",String.valueOf(entity.get().getCr_id()));
            if(entity.get().getIs_praise() == 1){
                viewModel.requestData(Constants.CANCEL_CIRCLE_PRAISE);
            }else{
                viewModel.requestData(Constants.ADD_CIRCLE_PRAISE);
            }
        }
    });

    /*
    * 子评论增加一条数据
    * */
    public void createData(PostCommentBean.ListBean.ListSBean childBean){
        CommentChildItemViewModel childItemViewModel = new CommentChildItemViewModel(viewModel,this,childBean);
        observableList.add(0,childItemViewModel);
    }
}
